package com.crois.barrier.web.plugin.before;

import com.crois.barrier.common.dto.ApiRouteInfo;
import com.crois.barrier.common.dto.IpBlackWhite;
import com.crois.barrier.common.enums.PluginEnum;
import com.crois.barrier.common.enums.PluginTypeEnum;
import com.crois.barrier.common.exception.BarrierException;
import com.crois.barrier.common.exception.BarrierExceptionEnum;
import com.crois.barrier.common.utils.IpUtil;
import com.crois.barrier.plugin.BarrierPluginChain;
import com.crois.barrier.web.cache.CacheManager;
import com.crois.barrier.web.plugin.AbstractBarrierPlugin;
import com.crois.barrier.web.support.ServerWebExchangeUtils;
import com.zhimydou.ztools.collection.CollectionUtil;
import com.zhimydou.ztools.net.IPUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Objects;


/**
 * @author Hou Ze Yu
 * @description ip黑白名单配置
 * @date 2020/5/7
 */
public class IpBlackWhitePlugin extends AbstractBarrierPlugin {

    private static final Logger LOGGER = LoggerFactory.getLogger(IpBlackWhitePlugin.class);

    private CacheManager cacheManager;

    public IpBlackWhitePlugin(CacheManager cacheManager) {
        super(cacheManager);
        this.cacheManager = cacheManager;
    }

    @Override
    public PluginTypeEnum pluginType() {
        return PluginTypeEnum.BEFORE;
    }

    @Override
    public String name() {
        return PluginEnum.IP_BLACK_WHITE.getName();
    }

    @Override
    public int order() {
        return PluginEnum.IP_BLACK_WHITE.getCode();
    }

    @Override
    public Boolean skip(ServerWebExchange exchange) {
        ApiRouteInfo routeInfo = (ApiRouteInfo) exchange.getAttributes().get(ServerWebExchangeUtils.BARRIER_ROUTE_ATTR);
        if (CollectionUtil.isNotEmpty(routeInfo.getPlugins())){
            return !routeInfo.getPlugins().contains(PluginEnum.IP_BLACK_WHITE.getName());
        }else {
            return true;
        }
    }

    @Override
    protected Mono<Void> doExecute(ServerWebExchange exchange, BarrierPluginChain chain, ApiRouteInfo routeInfo) {
        LOGGER.info("[开始执行：IpBlackWhitePlugin]");
        IpBlackWhite blackWhite = cacheManager.findBlackWhite();
        String ipAddr = IpUtil.getIpAddress(exchange.getRequest());
        if (!Objects.isNull(blackWhite)){
            // 先处理 黑名单
            if (CollectionUtil.isNotEmpty(blackWhite.getBlack())){

                boolean contains = blackWhite.getBlack().contains(ipAddr);
                // 如果 在黑名单里面 查看 是否 在 白名单里
                if (contains){
                    contains = blackWhite.getWhite().contains(ipAddr);
                    if (!contains){
                        LOGGER.error("[barrier ip black white plugin],ip:{},被限制",ipAddr);
                        throw new BarrierException(BarrierExceptionEnum.BARRIER_GATEWAY_IP_BLACK_FAIL);
                    }
                }
            }

            // 处理 白名单
            if (CollectionUtil.isNotEmpty(blackWhite.getWhite())) {
                boolean contains = blackWhite.getWhite().contains(ipAddr);
                if (!contains) {
                    LOGGER.error("[barrier ip black white plugin],ip:{},被限制",ipAddr);
                    throw new BarrierException(BarrierExceptionEnum.BARRIER_GATEWAY_IP_BLACK_FAIL);
                }
            }else {
                LOGGER.error("[barrier ip black white plugin],ip:{},被限制",ipAddr);
                throw new BarrierException(BarrierExceptionEnum.BARRIER_GATEWAY_IP_BLACK_FAIL);
            }
        }else {
            LOGGER.error("[barrier ip black white plugin],ip:{},被限制",ipAddr);
            throw new BarrierException(BarrierExceptionEnum.BARRIER_GATEWAY_IP_BLACK_FAIL);
        }
        LOGGER.info("[执行完成：IpBlackWhitePlugin]");
        return chain.execute(exchange);
    }
}
